package com.gmss.crawler.module.publication.service.impl;

import com.gmss.crawler.enums.AcademicianEnum;
import com.gmss.crawler.module.patent.domain.Patent;
import com.gmss.crawler.module.publication.domain.Publication;
import com.gmss.crawler.module.publication.mapper.PublicationMapper;
import com.gmss.crawler.module.publication.service.IPublicationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gmss.crawler.utils.ExcelUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 出版物表 服务实现类
 * </p>
 *
 * @author Auto-generator
 * @since 2021-10-24
 */
@Service
public class PublicationServiceImpl extends ServiceImpl<PublicationMapper, Publication> implements IPublicationService {

    @Resource
    private PublicationMapper publicationMapper;

    @Override
    public List<Publication> queryExportExcelData(Map<String, Object> map) {
        List<Publication> list = publicationMapper.queryPublicationData(map);
        return list;
    }

    @Override
    public boolean writeExcel(String filePath, String fileName, List<Publication> publicationList) {
        List<LinkedHashMap<String, Object>> list = convertExcel(publicationList);
        return ExcelUtils.saveExcel(fileName, filePath, list);
    }

    public List<LinkedHashMap<String, Object>> convertExcel(List<?> lists) {
        List<LinkedHashMap<String, Object>> rows = new ArrayList<>();
        Object object = lists.get(0);
        if (object instanceof Publication) {
            List<Publication> list = (List<Publication>) lists;
            for (Publication publication : list) {
                LinkedHashMap<String, Object> row = new LinkedHashMap<>();
                row.put("标识", publication.getPersonId() != null ? publication.getPersonId() : "");
                row.put("姓名", StringUtils.isNotBlank(publication.getName()) ? publication.getName() : "");
                row.put("头衔", StringUtils.isNotBlank(publication.getAcademicianTitleValue()) ? publication.getAcademicianTitleValue() : "");
                row.put("出版物名称", StringUtils.isNotBlank(publication.getTitle()) ? publication.getTitle() : "");
                row.put("作者", StringUtils.isNotBlank(publication.getAuthors()) ? publication.getAuthors() : "");
                row.put("关键词", StringUtils.isNotBlank(publication.getKeyword()) ? publication.getKeyword() : "");
                row.put("发表日期", StringUtils.isNotBlank(publication.getPublicationDate()) ? publication.getPublicationDate() : "");
                row.put("总页数", StringUtils.isNotBlank(publication.getPages()) ? publication.getPages() : "");

                rows.add(row);
            }
        }
        return rows;
    }
}
